import CryptoJS from "crypto-js/crypto-js";
import NJS from "./nhjssdk"
import moment from "moment";
export default {
    setLocalData: function (key, value) {

        if (localStorage) {
            localStorage.setItem(key, value);
        } else {
            sessionStorage.setItem(key, value);
        }
    },
    getLocalData: function (key) {
        if (localStorage) {
            var value = localStorage.getItem(key);
            return value;
        } else {
            var value = sessionStorage.getItem(key);
            return value;
        }
    },
    log(logMsg) {
        console.log(logMsg)
    },
    logToStr(logInfo) {
        this.log(JSON.stringify(logInfo));
    },
    //crypto aes加密工具对象
    crypto: {
        // 默认的 KEY 与 iv 如果没有给
        KEY: CryptoJS.enc.Utf8.parse("newhopetracy2019"),
        IV: CryptoJS.enc.Utf8.parse("newhopetracy2019"),
        Encrypt: function (word, keyStr, ivStr) {
            let key = this.KEY;
            let iv = this.IV;
            if (keyStr) {
                key = CryptoJS.enc.Utf8.parse(keyStr);
                iv = CryptoJS.enc.Utf8.parse(ivStr);
            }

            let srcs = CryptoJS.enc.Utf8.parse(word);
            var encrypted = CryptoJS.AES.encrypt(srcs, key, {
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.ZeroPadding
            });
            // console.log("-=-=-=-", encrypted.ciphertext);
            var hexStr = encrypted.ciphertext.toString().toUpperCase();

            // console.log("hexStr->" + hexStr);

            return hexStr;
        },
        Decrypt: function (word, keyStr, ivStr) {
            let key = this.KEY;
            let iv = this.IV;

            if (keyStr) {
                key = CryptoJS.enc.Utf8.parse(keyStr);
                iv = CryptoJS.enc.Utf8.parse(ivStr);
            }

            // let base64 = CryptoJS.enc.Base64.parse(word);
            let hexStr = CryptoJS.enc.Hex.parse(word);

            let src = CryptoJS.enc.Base64.stringify(hexStr);

            var decrypt = CryptoJS.AES.decrypt(src, key, {
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.ZeroPadding
            });

            var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
            return decryptedStr.toString();
        }
    },
    moment
}